<template>
  <div class="login-background">
    <div class="login-container">
      <div>login</div>
      <el-form :model="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-row class="flex justify-center mt-4">
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { loginAPI } from '@/api/user'
import { useRouter } from 'vue-router'
import { exportedModules } from '@/store/index'
const { useTokenStore } = exportedModules
const router = useRouter()
const tokenStore = useTokenStore()
const loginForm = ref({
  username: 'admin',
  password: '123456'
})

const handleLogin = async () => {
  const res = await loginAPI({
    name: loginForm.value.username,
    password: loginForm.value.password
  })
  tokenStore.setToken(res.data.token)
  router.push({ path: '/dashboard' })
  ElMessage({
    message: '登录成功',
    type: 'success'
  })
}
</script>

<style lang="scss" scoped>
.login-background {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/login/login-bg-2.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  overflow: hidden;
}

.login-container {
  width: 350px;
  height: 280px;
  padding: 20px;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-form {
  width: 100%;
}
</style>
